/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kurvykurvy.page;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import org.openide.util.Exceptions;

/**
 *
 * @author HY
 */
public class PageLayoutTemplate
        implements Transferable
{
    public final static String MIME_TYPE = "application/kurvy-page-layout; class=" + PageLayoutTemplate.class.getCanonicalName();
    public static DataFlavor PAGE_LAYOUT_FLAVOR;

    public final static int LEFT_PAGE = 2;
    public final static int RIGHT_PAGE = 4;

    static
    {
        try
        {
            PAGE_LAYOUT_FLAVOR = new DataFlavor( MIME_TYPE );
        }
        catch ( ClassNotFoundException ex )
        {
            Exceptions.printStackTrace( ex );
        }
    }

    BufferedImage pageMask;
    BufferedImage templateDecorations;
    BufferedImage previewImage;

    boolean background;

    Point imageOffset = new Point( 0, 0 );
    Dimension actualTemplateSize = new Dimension( 0, 0 );

    boolean left;

    ArrayList<ImageFrameTemplate> imageFrames;

    public PageLayoutTemplate( BufferedImage pageMask, BufferedImage templateDecoration, int type )
    {
        switch( type )
        {
            case LEFT_PAGE:
                left = true;
                break;
            case RIGHT_PAGE:
                left = false;
                break;
        }
        this.pageMask = pageMask;
        this.templateDecorations = templateDecoration;
    }

    public PageLayoutTemplate( BufferedImage pageMask, BufferedImage templateDecorations, ArrayList<ImageFrameTemplate> imageFrames, int type )
    {
        this( pageMask, templateDecorations, type );
        this.imageFrames = imageFrames;
    }

    public void setTemplateDecorations( BufferedImage decorations )
    {
        this.templateDecorations = decorations;
    }

    public BufferedImage getTemplateDecorations()
    {
        return this.templateDecorations;
    }

    public void setOffsetPoint( Point p )
    {
        this.imageOffset = p;
    }

    public Point getOffsetPoint()
    {
        return this.imageOffset;
    }

    public Dimension getActualTemplateSize()
    {
        return actualTemplateSize;
    }

    public void setActualTemplateSize( Dimension actualTemplateSize )
    {
        this.actualTemplateSize = actualTemplateSize;
    }

    public void addImageFrame( ImageFrameTemplate frame )
    {
        if( this.imageFrames == null )
            this.imageFrames = new ArrayList<ImageFrameTemplate>();

        this.imageFrames.add( frame );
    }

    public void addImageFrame( ArrayList<ImageFrameTemplate> frames )
    {
        if( this.imageFrames == null )
            this.imageFrames = new ArrayList<ImageFrameTemplate>();

        this.imageFrames.addAll( frames );
    }

    public void setImageFrames( ArrayList<ImageFrameTemplate> frames )
    {
        this.imageFrames = frames;
    }

    public ArrayList<ImageFrameTemplate> getImageFrames()
    {
        ArrayList<ImageFrameTemplate> result = new ArrayList<ImageFrameTemplate>();
        if( this.imageFrames != null )
        {
            result.addAll( this.imageFrames );
        }
        return result;
    }

    public boolean isLeft()
    {
        return left;
    }

    public void setLeft()
    {
        this.left = true;
    }

    public void setRight()
    {
        this.left = false;
    }

    public BufferedImage getPreviewImage()
    {
        if( previewImage == null )
        {
            BufferedImage pageMaskImg = this.pageMask;

            previewImage = new BufferedImage( pageMaskImg.getWidth(), pageMaskImg.getHeight(), BufferedImage.TYPE_INT_RGB );
            Graphics2D g2 = previewImage.createGraphics();

            g2.drawImage( this.templateDecorations, null, 0, 0 );
            g2.drawImage(  this.pageMask, null, 0, 0 );
            
            g2.dispose();
        }
        return previewImage;
    }

    public DataFlavor[] getTransferDataFlavors()
    {
        return new DataFlavor[]{ PAGE_LAYOUT_FLAVOR };
    }

    public boolean isDataFlavorSupported( DataFlavor flavor )
    {
        if( flavor != null && MIME_TYPE.equalsIgnoreCase( flavor.getMimeType() ) )
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public Object getTransferData( DataFlavor flavor ) throws UnsupportedFlavorException, IOException
    {
        if( isDataFlavorSupported( flavor ) )
        {
            return this;
        }
        else
        {
            return null;
        }
    }

    public void setBackgroundOnly( boolean background )
    {
        background = true;
    }

    public boolean isBackground()
    {
        return background;
    }

    public boolean isMask()
    {
        return !background;
    }
}
